package com.lishu.carsafe.activity;

import android.text.TextUtils;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.lishu.carsafe.R;
import com.lishu.carsafe.model.entity.Lonlat;

import java.util.ArrayList;
import java.util.List;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/6/15
 * time:15:55
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class BaseMapActivity extends BaseActivity {

    protected MapView mMapView;
    protected BaiduMap mBaiduMap;
    protected LocationClient mLocationClient;
    protected UiSettings mUiSettings;
    protected boolean isFirstLoc = true;

    protected void initView(){
        //获取地图控件引用
        mMapView = findViewById(R.id.mv_view);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

    }


    @Override
    protected void onStart() {
        // 如果要显示位置图标,必须先开启图层定位
        mBaiduMap.setMyLocationEnabled(true);
        if (!mLocationClient.isStarted()) {
            mLocationClient.start();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时必须调用mMapView. onResume ()
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时必须调用mMapView. onPause ()
        mMapView.onPause();
    }

    @Override
    protected void onStop() {
        // 关闭图层定位
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时必须调用mMapView.onDestroy()
        mMapView.onDestroy();
    }

    public void initMapLocation() {
        //实例化UiSettings类对象
        mUiSettings = mBaiduMap.getUiSettings();

        //定位初始化
        mLocationClient = new LocationClient(this);

        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(0);//如果为0 定位一次

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
//        MyLocationListener myLocationListener = new MyLocationListener();
//        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();

    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null) {
                return;
            }
//            MyLocationData locData = new MyLocationData.Builder()
//                    .accuracy(location.getRadius())
//                    // 此处设置开发者获取到的方向信息，顺时针0-360
//                    .direction(location.getDirection()).latitude(location.getLatitude())
//                    .longitude(location.getLongitude()).build();
//            mBaiduMap.setMyLocationData(locData);//给地图设置定位数据，这样地图就显示位置了
//            /**
//             *当首次定位时，记得要放大地图，便于观察具体的位置
//             * LatLng是缩放的中心点，这里注意一定要和上面设置给地图的经纬度一致；
//             * MapStatus.Builder 地图状态构造器
//             */
//            if (isFirstLoc) {
//                isFirstLoc = false;
//
//                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
//                MapStatus.Builder builder = new MapStatus.Builder();
//                //设置缩放中心点；缩放比例；
//                builder.target(ll).zoom(20.0f);
//                //给地图设置状态
//                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//            }
        }
    }

    /**
     * 批量增加点
     *
     * @param detail
     */
    protected void drawPoints(List<Lonlat> detail) {

        if (detail != null  && detail.size() > 0) {
            BitmapDescriptor bdGround = BitmapDescriptorFactory.fromResource(R.mipmap.icon_spot);
            //构建Marker图标
            BitmapDescriptor startBitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_start);
            //构建Marker图标
            BitmapDescriptor lastBitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_stop);

            //创建OverlayOptions的集合
            List<OverlayOptions> options = new ArrayList<>(); //站点图标标记
            List<OverlayOptions> textOptions = new ArrayList<>();//站点文字标记
            List<Lonlat> lonlats = detail;
            List<LatLng> latLngs = new ArrayList<>();

            LatLng ll = new LatLng(detail.get(0).getBdLat(), detail.get(0).getBdLon());
            MapStatus.Builder builder = new MapStatus.Builder();
            //设置缩放中心点；缩放比例；
            builder.target(ll).zoom(21.0f);
            //给地图设置状态
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            for (int i = 0; i < lonlats.size(); i++) {
                //构造大量坐标数据
                LatLng point = new LatLng(lonlats.get(i).getBdLat(), lonlats.get(i).getBdLon());
                latLngs.add(point);
                //创建OverlayOptions属性
                OverlayOptions overlayOptions = null;
                if(i == 0){
                    overlayOptions = new MarkerOptions()
                            .position(point)
                            .icon(startBitmap);
                }else if(i == lonlats.size()-1){
                    overlayOptions = new MarkerOptions()
                            .position(point)
                            .icon(lastBitmap);
                }else{
                    overlayOptions = new MarkerOptions()
                            .position(point)
                            .icon(bdGround);
                }
                //将OverlayOptions添加到list
                options.add(overlayOptions);

                if(i != 0 && i != lonlats.size()-1 && lonlats.get(i).getType() == 2 && !TextUtils.isEmpty(lonlats.get(i).getStation().getName())) {
                    //构建TextOptions对象
                    LatLng textPoint = new LatLng(lonlats.get(i).getBdLat(), lonlats.get(i).getBdLon() + 0.00011);
                    OverlayOptions mTextOptions = new TextOptions()
                            .text(lonlats.get(i).getStation().getName()) //文字内容
                            .bgColor(0xFFFFFFFF) //背景色
                            .fontSize(22) //字号
                            .fontColor(0xFF333333) //文字颜色
                            .rotate(0.1f) //旋转角度
                            .position(textPoint);

                    //在地图上显示文字覆盖物
                    textOptions.add(mTextOptions);
                }
            }
            //画线
            OverlayOptions mOverlayOptions = new PolylineOptions()
                    .width(25)
                    .color(0xFF00ADB5)//0xAAFF0000
                    .points(latLngs);
            mBaiduMap.addOverlay(mOverlayOptions);

            //画点
            mBaiduMap.addOverlays(options);
            //画点文字
            mBaiduMap.addOverlays(textOptions);
        }
    }
}
